package longrise.phone.com.bjjt_jyb.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.media.MediaPlayer;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.VideoView;

import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;

import longrise.phone.com.bjjt_jyb.R;
import longrise.phone.com.bjjt_jyb.Utils.CommontUtils;
import longrise.phone.com.bjjt_jyb.Utils.ImageLoaderForSettingUtils;

/**
 * 拍照，录音，视频的自定义组件。
 */
public class PicModule extends LinearLayout{

    private final Context context;
    private Integer picAttr=0;//0为拍照，1为录音，2为视频
    private ImageView iv_pic;
    private TextView tv_piclocation;
    private FrameLayout fl_record;
    private ImageView iv_record;
    private ImageView iv_recordplay;
    private Button bt_record;
    private RelativeLayout rl_video;
    private VideoView vv_video;
    private TextView tv_videotext;

    public PicModule(Context context) {
        this(context, null);
    }

    public PicModule(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PicModule(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.PicModule);
        picAttr = ta.getInteger(R.styleable.PicModule_picAttr, 0);
        ta.recycle();
        this.context = context;
        init(context);
    }

    private void init(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.picmoduleview, this);
        //拍照
        iv_pic = (ImageView) view.findViewById(R.id.iv_pic);
        tv_piclocation = (TextView) view.findViewById(R.id.tv_piclocation);
        //录音
        fl_record = (FrameLayout) view.findViewById(R.id.fl_record);
        iv_record = (ImageView) view.findViewById(R.id.iv_record);
        iv_recordplay = (ImageView) view.findViewById(R.id.iv_recordplay);
        bt_record = (Button) view.findViewById(R.id.bt_record);
        //视频
        rl_video = (RelativeLayout) view.findViewById(R.id.rl_video);
        vv_video = (VideoView) view.findViewById(R.id.vv_video);
        tv_videotext = (TextView) view.findViewById(R.id.tv_videotext);
        switch (picAttr) {
            case 0:
                fl_record.setVisibility(View.GONE);
                iv_record.setVisibility(View.GONE);
                iv_recordplay.setVisibility(View.GONE);
                bt_record.setVisibility(View.GONE);
                rl_video.setVisibility(View.GONE);
                vv_video.setVisibility(View.GONE);
                tv_videotext.setVisibility(View.GONE);
                break;
            case 1:
                iv_pic.setVisibility(View.GONE);
                tv_piclocation.setVisibility(View.GONE);
                rl_video.setVisibility(View.GONE);
                vv_video.setVisibility(View.GONE);
                tv_videotext.setVisibility(View.GONE);
                break;
            case 2:
                iv_pic.setVisibility(View.GONE);
                tv_piclocation.setVisibility(View.GONE);
                fl_record.setVisibility(View.GONE);
                iv_record.setVisibility(View.GONE);
                iv_recordplay.setVisibility(View.GONE);
                bt_record.setVisibility(View.GONE);
                break;
        }

    }



    /********************************** 拍照组件相关方法 ***************************************/
    /**
     * 初始化图片和文字
     *
     * @param text
     */
    public void setModleParameter(int resoureId,String text) {
        iv_pic.setImageResource(resoureId);
        //ImageLoaderForSettingUtils.getInstance(context).displayFromMipmap(resoureId,iv_pic);
        tv_piclocation.setText(text);
    }
    /**
     * 为背景设置bitmap,照片拍摄成功后调用显示图片
     *
     * @param b
     */
    public void setModuleBackground(String b) {
        ImageLoaderForSettingUtils.getInstance(context).displayFromSDCardAddListener(b, iv_pic, new ImageLoadingListener() {
            @Override
            public void onLoadingStarted(String s, View view) {

            }

            @Override
            public void onLoadingFailed(String s, View view, FailReason failReason) {

            }

            @Override
            public void onLoadingComplete(String s, View view, Bitmap bitmap) {
                int width = bitmap.getWidth();
                int height = bitmap.getHeight();
                if(width>height) iv_pic.setImageBitmap(bitmap);
                else {
                    Bitmap photos = CommontUtils.createPhotos(bitmap);
                    iv_pic.setImageBitmap(photos);
                }
            }

            @Override
            public void onLoadingCancelled(String s, View view) {

            }
        });
    }



    /***
     * 部分方法需要imageview
     * @return
     */
    public ImageView getIv_pic() {
        return iv_pic;
    }



    /********************************** 录音组件相关方法 ***************************************/
    public ImageView getIv_record() {
        return iv_record;
    }
    public int getIv_recordId() {
        return iv_record.getId();
    }

    public ImageView getIv_recordplay() {
        return iv_recordplay;
    }

    public Button getBt_record() {
        return bt_record;
    }

    /************************************* 视频组件相关方法 ******************************************/
    /***
     * 获取视频控件
     * @return
     */
    public VideoView getVv_video() {
        return vv_video;
    }

}
